package com.ly.hotel.spainterface.jxd.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.spainterface.NmSwipeCardUtils;
import com.ly.hotel.spainterface.OnSwipeCardInterface;
import com.ly.hotel.spainterface.jxd.activity.JxdXiaoFeiActivity;
import com.ly.hotel.spainterface.nm.event.XFReqCardEvent;
import com.ly.hotel.spainterface.nm.event.XFResCardEvent;

import de.greenrobot.event.EventBus;

/**
 * Created by SHUBEN on 2018/3/23.
 */

public class JxdStaffSwipeDialog extends Dialog {
    private JxdXiaoFeiActivity mcontext;
    private LinearLayout myLayout;
    private NmSwipeCardUtils swipUtils;


    public JxdStaffSwipeDialog(JxdXiaoFeiActivity context, int theme) {
        super(context, theme);
        mcontext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.nm_xf_buy_item);

        swipUtils =  new NmSwipeCardUtils(new OnSwipeCardInterface() {
            @Override
            public void swipeCardListener(String cardId) {
                String newId = cardId.replaceAll("\r\n", "");
                if(newId.length() == 10) {
                    try {
                        long b = Long.parseLong(newId);
                        EventBus.getDefault().post(new XFReqCardEvent(2, newId));
                    } catch (NumberFormatException e) {
                        Log.e("swipe", "刷卡含有非法字符，重刷！");
                        swipUtils.initSwipe();
                    }
                }else{
                    Log.e("swipe", "刷卡不足10位，重刷！");
                    swipUtils.initSwipe();
                }
            }
        });

        myLayout = (LinearLayout) findViewById(R.id.swipe_parent_layout);
        myLayout.removeAllViews();
        TextView view = new TextView(mcontext);
        int hei = mcontext.getResources().getDimensionPixelOffset(R.dimen.px210);
        int ts =  mcontext.getResources().getDimensionPixelOffset(R.dimen.px36);
        view.setTextSize(TypedValue.COMPLEX_UNIT_PX,ts);
        view.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,hei);
        layoutParams.gravity = Gravity.CENTER;
        view.setLayoutParams(layoutParams);
        view.setText(mcontext.getResources().getString(R.string.xf_staff_swipe));
        myLayout.addView(view);

        Button backBtn = (Button) findViewById(R.id.dialog_back);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dismiss();
            }
        });
        swipUtils.initSwipe();
    }

    //收到手牌反馈
    public void onEventMainThread(XFResCardEvent e) {
        if(e.getType() == 2) {
            if (e.getTag()){
                JxdStaffOperationDialog operDialog = new JxdStaffOperationDialog(mcontext, R.style.introDialog);
                operDialog.setCanceledOnTouchOutside(true);
                operDialog.show();
                dismiss();
            }
        }
    }

    @Override
    protected void onStop() {
        swipUtils.setOnSwipeFlag(false);
        EventBus.getDefault().unregister(this);
        super.onStop();
    }
}